package com.wt.chat.context;

import java.util.HashMap;
import java.util.Map;

/**
 * 持有一些上下文信息
 * @author: wangtao
 * @date:17:19 2019/6/28
 * @email:taow02@jumei.com
 */
public class ContextHolder {

	private static final ThreadLocal<Map<String, Object>> DATA_MAP = ThreadLocal.withInitial(HashMap::new);

	/**
	 * 预置Key：当前登录客服
	 */
	private static final String CURRENT_KEFU_ID_KEY = "current_kefu_id";

	/**
	 * 预置Key：当前登录客服
	 */
	private static final String CURRENT_COMPANY_ID_KEY = "current_company_id";

	/**
	 * 向上下文添加自定义数据
	 * @param key 键
	 * @param value 值
	 */
	public static void putData(String key, Object value) {
		DATA_MAP.get().put(key, value);
	}

	/**
	 * 从上下文获取自定义数据
	 * @param key 数据键值
	 * @return
	 */
	public static Object getData(String key) {
		return DATA_MAP.get().get(key);
	}

	/**
	 * 销毁上下文数据
	 */
	public static void destroy() {
		DATA_MAP.set(null);
	}

	/**
	 * 注册当前登录kefuId
	 * @param kefuId
	 */
	public static void registerCurrentKefuId(Integer kefuId) {
		putData(CURRENT_KEFU_ID_KEY, kefuId);
	}

	/**
	 * 获取当前登录kefuId
	 */
	public static Integer currentKefuId() {
		return (Integer) getData(CURRENT_KEFU_ID_KEY);
	}

	/**
	 * 注册当前corpId
	 * @param corpId
	 */
	public static void registerCurrentCompanyId(Integer corpId) {
		putData(CURRENT_COMPANY_ID_KEY, corpId);
	}

	/**
	 * 获取当前corpId
	 */
	public static Integer currentCompanyId() {
		return (Integer) getData(CURRENT_COMPANY_ID_KEY);
	}

}
